package com.zju.searchEngine.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zju.searchEngine.mapper.IndexMapper;
import com.zju.searchEngine.model.Index;
import com.zju.searchEngine.model.IndexExample;
import com.zju.searchEngine.service.IIndexService;

@Service
public class IndexService implements IIndexService {
	
	@Autowired
	private IndexMapper indexMapper;

	@Override
	public Index selectByKeyWord(String keyWord) {
		IndexExample indexExample = new IndexExample();
		indexExample.createCriteria().andKeywordEqualTo(keyWord);
		//System.out.println(indexMapper.selectByExample(indexExample).size());
		if(indexMapper.selectByExample(indexExample).size()>0){
			return indexMapper.selectByExample(indexExample).get(0);
		}
		else{
			return null;
		}
	}

	@Override
	public Index selectById(Integer id) {
		return indexMapper.selectByPrimaryKey(id);
	}

	@Override
	public int insert(Index index) {
		return indexMapper.insert(index);
	}

	@Override
	public List<Index> selectAllIndex() {
		IndexExample indexExample = new IndexExample();
		return indexMapper.selectByExample(indexExample);
	}

	@Override
	public int updateById(Index index) {
		return indexMapper.updateByPrimaryKey(index);
	}

	@Override
	public List<Index> selectByKeyWordLike(String keyWord) {
		IndexExample indexExample = new IndexExample();
		indexExample.createCriteria().andKeywordLike("%"+keyWord+"%");
		return indexMapper.selectByExample(indexExample);
	}

}
